home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / MPlay / mpeg_play2.0 / 24bit.c next >
Encoding:
C/C++ Source or Header  |  1995-12-04  |  4.8 KB  |  213 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. /* from  MPEG Video Software Decoder (Version 2.0; Jan 27, 1993)
  23.  * p griffin 12/95
  24.  */
  25.  
  26. #include "video.h"
  27. /*#include "dither.h"*/
  28. #include "proto.h"
  29.  
  30. /*
  31.  * We'll define the "ConvertColor" macro here to do fixed point arithmetic
  32.  * that'll convert from YCrCb to RGB using:
  33.  *    R = L + 1.40200*Cr;
  34.  *    G = L - 0.34414*Cb - 0.71414*Cr
  35.  *    B = L + 1.77200*Cb;
  36.  *
  37.  * We'll use fixed point by adding two extra bits after the decimal.
  38.  */
  39.  
  40. #define BITS    8
  41. #define ONE     ((int) 1)
  42. #define CONST_SCALE    (ONE << BITS)
  43. #define ROUND_FACTOR    (ONE << (BITS-1))
  44.  
  45. /* Macro to convert integer to fixed. */
  46. #define UP(x)    (((int)(x)) << BITS)
  47.  
  48. /* Macro to convert fixed to integer (with rounding). */
  49. #define DOWN(x)    (((x) + ROUND_FACTOR) >> BITS)
  50.  
  51. /* Macro to convert a float to a fixed */
  52. #define FIX(x)  ((int) ((x)*CONST_SCALE + 0.5))
  53.  
  54. #define CLAMP(ll,x,ul)    ( ((x)<(ll)) ?(ll):( ((x)>(ul)) ?(ul):(x)))
  55.  
  56. static int *Cb_r_tab, *Cr_g_tab, *Cb_g_tab, *Cr_b_tab;
  57.  
  58. /*
  59.  *--------------------------------------------------------------
  60.  *
  61.  * InitColorDither --
  62.  *
  63.  *    To get rid of the multiply and other conversions in color
  64.  *    dither, we use a lookup table.
  65.  *
  66.  * Results:
  67.  *    None.
  68.  *
  69.  * Side effects:
  70.  *    The lookup tables are initialized.
  71.  *
  72.  *--------------------------------------------------------------
  73.  */
  74.  
  75. void
  76. InitColorDither()
  77. {
  78.     int CR, CB, i;
  79.  
  80.     Cr_b_tab = (int *)malloc(256*sizeof(int));
  81.     Cr_g_tab = (int *)malloc(256*sizeof(int));
  82.     Cb_g_tab = (int *)malloc(256*sizeof(int));
  83.     Cb_r_tab = (int *)malloc(256*sizeof(int));
  84.  
  85.     for (i=0; i<256; i++) {
  86.     CB = CR = i;
  87.  
  88.     CB -= 128; CR -= 128;
  89.  
  90.     Cb_r_tab[i] = FIX(1.40200) * CB;
  91.     Cr_g_tab[i] = -FIX(0.34414) * CR;
  92.     Cb_g_tab[i] = -FIX(0.71414) * CB;   
  93.     Cr_b_tab[i] = FIX(1.77200) * CR;
  94.     }
  95.  
  96.  
  97.     free(Cr_b_tab);
  98.     free(Cr_g_tab);
  99.     free(Cb_g_tab);
  100.     free(Cb_r_tab);
  101. }
  102.  
  103.  
  104. /*
  105.  *--------------------------------------------------------------
  106.  *
  107.  * ColorDitherImage --
  108.  *
  109.  *    Converts image into 24 bit color.
  110.  *
  111.  * Results:
  112.  *    None.
  113.  *
  114.  * Side effects:
  115.  *    None.
  116.  *
  117.  *--------------------------------------------------------------
  118.  */
  119.  
  120. void
  121. ColorDitherImage(lum, cr, cb, out, rows, cols)
  122.   unsigned char *lum;
  123.   unsigned char *cr;
  124.   unsigned char *cb;
  125.   unsigned char *out;
  126.   int cols, rows;
  127.  
  128. {
  129.     int L, CR, CB;
  130.     unsigned int *row1, *row2;
  131.     unsigned char *lum2;
  132.     int x, y;
  133.     unsigned int r, b, g;
  134.     int cb_r;
  135.     int cr_g;
  136.     int cb_g;
  137.     int cr_b;
  138.  
  139.     row1 = (unsigned int *)out;
  140.     row2 = row1 + cols;
  141.     lum2 = lum + cols;
  142.     for (y=0; y<rows; y+=2) {
  143.     for (x=0; x<cols; x+=2) {
  144.         int R, G, B;
  145.  
  146.         CR = *cr++;
  147.         CB = *cb++;
  148.         cb_r = Cb_r_tab[CB];
  149.         cr_g = Cr_g_tab[CR];
  150.         cb_g = Cb_g_tab[CB];
  151.         cr_b = Cr_b_tab[CR];
  152.  
  153.         L = *lum++;
  154.         L = UP(L);
  155.         R = L + cb_r;
  156.         G = L + cr_g + cb_g;
  157.         B = L + cr_b;
  158.  
  159.         r = CLAMP(0,R,UP(255)) >> BITS;
  160.         g = CLAMP(0,G,UP(255)) & 0xff00;
  161.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  162.  
  163.         *row1++ = r | g | b;
  164.  
  165.         L = *lum++;
  166.         L = UP(L);
  167.         R = L + cb_r;
  168.         G = L + cr_g + cb_g;
  169.         B = L + cr_b;
  170.  
  171.         r = CLAMP(0,R,UP(255)) >> BITS;
  172.         g = CLAMP(0,G,UP(255)) & 0xff00;
  173.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  174.  
  175.         *row1++ = r | g | b;
  176.  
  177.         /*
  178.          * Now, do second row.
  179.          */
  180.         L = *lum2++;
  181.         L = UP(L);
  182.         R = L + cb_r;
  183.         G = L + cr_g + cb_g;
  184.         B = L + cr_b;
  185.  
  186.         r = CLAMP(0,R,UP(255)) >> BITS;
  187.         g = CLAMP(0,G,UP(255)) & 0xff00;
  188.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  189.  
  190.         *row2++ = r | g | b;
  191.  
  192.         L = *lum2++;
  193.         L = UP(L);
  194.         R = L + cb_r;
  195.         G = L + cr_g + cb_g;
  196.         B = L + cr_b;
  197.  
  198.         r = CLAMP(0,R,UP(255)) >> BITS;
  199.         g = CLAMP(0,G,UP(255)) & 0xff00;
  200.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  201.  
  202.         *row2++ = r | g | b;
  203.     }
  204.     lum += cols;
  205.     lum2 += cols;
  206.     row1 += cols;
  207.     row2 += cols;
  208.     }
  209. }
  210.  
  211.  
  212.  
  213.